package omq.panel;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.prefs.Preferences;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import omq.api.OMQConnectionFactory;
import omq.api.OMQConnectionSOAP;
import omq.api.OMQQueue;
import omq.api.OMQQueueManager;
import omq.api.OMQSession;
import omq.api.logger.OMQLogger;
import omq.screen.OmqTesterMainScreen;

import component.OmqButton;

import constant.OmqTesterClientConst;

public class GetQueuePropertiesPanel extends OmqPanel {


	/**
	 * 
	 */
	private static final long serialVersionUID = -3305811054548239167L;
	private OmqButton QueuePropertiesBtn;
	private JLabel responseLbl;

	
	public GetQueuePropertiesPanel(LayoutManager arg0, boolean arg1) {
		super(arg0, arg1);
		initialize();
	}

	public GetQueuePropertiesPanel(LayoutManager arg0) {
		super(arg0);
		initialize();
	}

	public GetQueuePropertiesPanel(boolean arg0) {
		super(arg0);
		initialize();
	}

	public GetQueuePropertiesPanel() {
		super();
		initialize();
	}
	
	private void initialize(){
		
		setLayout(new GridBagLayout());
		getInfoLbl().setText("<html><body>" + getTitle("Get Queue Properties") + "<br>Use this method to retreive the properties of the queue from the online-mq server.<br><br><br><br></body></html>");
		
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.anchor = GridBagConstraints.LINE_START;
		c.gridx = 0;
		
		c.gridy = 0;
		
		// changing label size
		infoLbl.setPreferredSize(new Dimension(450,90));
		
		add(getInfoLbl(),c);
		
		c.gridy = 1;
		c.insets = new Insets(10,0,0,0);
		OmqPanel omqPanel = new OmqPanel();
		omqPanel.add(getQueuePropertiesBtn(),c);
		omqPanel.add(new JLabel(""),c);
		add(omqPanel,c);
		
		c.gridy = 2;
		add(getResponseLbl(),c);
		
		c.gridy = 3;
		c.weighty = 1.0;
		add(new JLabel(""),c);
		initpref();
	}

	public OmqButton getQueuePropertiesBtn() {
		if(QueuePropertiesBtn == null){
			QueuePropertiesBtn = new OmqButton("Get Queue Properties");
			QueuePropertiesBtn.addActionListener(new ActionListener() {
			
				public void actionPerformed(ActionEvent arg0) {
					HashMap pref = OmqTesterMainScreen.prefMap;
					
					
					JOptionPane.showMessageDialog(new JFrame(),"<html><body>get queue properties for "+myPreferences.get(OmqTesterClientConst.PREF_QUEUE_NAME,OmqTesterClientConst.PREF_QUEUE_NAME)+", please wait ...</body></html>","Message",JOptionPane.INFORMATION_MESSAGE);
					waitForInfo(true);
					String response = null;
					OMQQueue queue = null;
					
					try {

						OMQLogger.getInstance().info("about to retreiev queue properties, please wait");

						conn = OMQConnectionFactory.getConnection(myPreferences.get(OmqTesterClientConst.PREF_USER_NAME, OmqTesterClientConst.PREF_USER_NAME),
								myPreferences.get(OmqTesterClientConst.PREF_PASSWORD, OmqTesterClientConst.PREF_PASSWORD),
								myPreferences.get(OmqTesterClientConst.PREF_SERVER_URL, OmqTesterClientConst.PREF_SERVER_URL),
								new OMQQueueManager(myPreferences.get(OmqTesterClientConst.PREF_QUEUE_MGR, OmqTesterClientConst.PREF_QUEUE_MGR)));

						OMQLogger.getInstance().info("user      = "+myPreferences.get(OmqTesterClientConst.PREF_USER_NAME, OmqTesterClientConst.PREF_USER_NAME));						
						OMQLogger.getInstance().info("qmgr      = "+myPreferences.get(OmqTesterClientConst.PREF_QUEUE_MGR,OmqTesterClientConst.PREF_QUEUE_MGR));						
						OMQLogger.getInstance().info("queue     = "+myPreferences.get(OmqTesterClientConst.PREF_QUEUE_NAME,OmqTesterClientConst.PREF_QUEUE_NAME));						
						OMQLogger.getInstance().info("server    = "+myPreferences.get(OmqTesterClientConst.PREF_SERVER_URL,OmqTesterClientConst.PREF_SERVER_URL));

						OMQSession sess = conn.createSession(false);
						queue = sess.getQueueDefinitions(new OMQQueue(myPreferences.get(OmqTesterClientConst.PREF_QUEUE_NAME,OmqTesterClientConst.PREF_QUEUE_NAME)));

					} catch (Exception e) {
						OMQLogger.getInstance().error(e.getMessage());
						JOptionPane.showMessageDialog(new JFrame(),"<html><body>" + e.getMessage() + "</body></html>","System error",JOptionPane.ERROR_MESSAGE);
						response = "<html><body><center><center><hr><h2 style='color:"+OmqTesterClientConst.GUIResponseFailedcolor+";font-size:10px;text-decoration:underline;Font-name: arial;width:450px;'>Get queue properties failed</h2><br>" + responseMsg;
						getResponseLbl().setText(response);
						waitForInfo(false);
					}
					
					 if(conn.getLastErrorCode() == 0){
 						 OMQLogger.getInstance().info("get queue properties ended succesfully");
 						 response = printQueueProperties(queue);
					 }else {
						OMQLogger.getInstance().info("get queue properties failed, rc = "+conn.getLastErrorCode()+" error description = "+conn.getLastErrorDescription());
						 response = printErrorFromOMQ("Get Queue Properties",true);
					 }
					 waitForInfo(false);
					 getResponseLbl().setText(response);
					 
				}
			});
		}
		return QueuePropertiesBtn;
	}

	public JLabel getResponseLbl() {
		if(responseLbl == null){
			responseLbl = new JLabel("");
		}
		return responseLbl;
	}
	
	public void resetPanel(){
		getResponseLbl().setText("");
	}
	

}
